package com.xuexiang.xuidemo.slice;
import com.xuexiang.xui_lib.component.dialog.config.ChooseBean;
import com.xuexiang.xui_lib.component.dialog.config.ConfigBean;
import com.xuexiang.xui_lib.component.dialog.interfaces.DialogListener;
import com.xuexiang.xui_lib.component.dialog.interfaces.ItemDialogListener;
import com.xuexiang.xui_lib.component.dialog.view.StyledDialog;
import com.xuexiang.xui_lib.util.LogUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;

import java.util.ArrayList;

/**
 * Dialog构建策略
 *
 * @since 2021-04-08
 */
public class DialogStrategyAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Image image_back;
    private DirectionalLayout dl_one, dl_two, dl_three, dl_four, dl_five, dl_six, dl_seven;
    ConfigBean configBean = null;
    private static final String TAG = DialogStrategyAbilitySlice.class.getSimpleName();
    private int mPositon;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_dialog_strategy);
        initView();
        initClickedListener();
    }

    public void initView() {
        image_back = (Image) findComponentById(ResourceTable.Id_image_back);
        dl_one = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_one);
        dl_two = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_two);
        dl_three = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_three);
        dl_four = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_four);
        dl_five = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_five);
        dl_six = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_six);
        dl_seven = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_seven);
    }

    private void initClickedListener() {
        image_back.setClickedListener(this);
        dl_one.setClickedListener(this);
        dl_two.setClickedListener(this);
        dl_three.setClickedListener(this);
        dl_four.setClickedListener(this);
        dl_five.setClickedListener(this);
        dl_six.setClickedListener(this);
        dl_seven.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                //System.exit(AppCfig.EXIT);
                onBackPressed();
                break;
            case ResourceTable.Id_dl_one:

                break;
            case ResourceTable.Id_dl_two:

                break;
            case ResourceTable.Id_dl_three:
                LogUtils.error(TAG, "Id_dl_three is click");
                configBean = StyledDialog.buildTipAlert(getContext(), "提示", "请注意，这是一个简单的提示性对话框！", "确定", "", new DialogListener() {
                    @Override
                    public void positiveButton() {
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;
                    }

                    @Override
                    public void negativeButton() {
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;
                    }
                });
                configBean.commonDialog.show();
                break;
            case ResourceTable.Id_dl_four:
                configBean = StyledDialog.buildTipAlert(getContext(), "", "是否允许打开蓝牙？", "是", "否", new DialogListener() {
                    @Override
                    public void positiveButton() {
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;
                        ToastUtil.showToast(getContext(), "同意打开蓝牙！");
                    }

                    @Override
                    public void negativeButton() {
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;
                        ToastUtil.showToast(getContext(), "不同意打开蓝牙！");
                    }
                });
                configBean.commonDialog.show();
                break;
            case ResourceTable.Id_dl_five:
                configBean = StyledDialog.buildNormalInput(getContext(), "警告", "请输入密码", new DialogListener() {

                    @Override
                    public void positiveButton() {
                        //可对输入内容进行校验
                        configBean.getInputText1();
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;
                    }

                    @Override
                    public void negativeButton() {
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;
                    }
                });
                configBean.commonDialog.show();

                break;
            case ResourceTable.Id_dl_six:
                CharSequence[] words1 = {"添加", "修改", "更新", "删除"};
                boolean[] selectItems1 = {true, false, false, false};

                ArrayList<ChooseBean> list1 = new ArrayList<>();
                for (int i = 0; i < words1.length; i++) {
                    ChooseBean chooseBean = new ChooseBean();
                    chooseBean.setTxt(words1[i]);
                    chooseBean.setChoosen(selectItems1[i]);
                    list1.add(chooseBean);
                }
                configBean = StyledDialog.buildMdIOSListOther(getContext(), list1, new ItemDialogListener() {
                    @Override
                    public void onItemClick(CharSequence text, int position) {
                        mPositon = position+1;
                        LogUtils.error(TAG, "Id_dl_six is click");
                        if (mPositon == 0) {
                            ToastUtil.showToast(getContext(),"选择了第1个");
                        } else {
                            ToastUtil.showToast(getContext(),"选择了第" + mPositon + "个");
                        }
                        configBean.commonDialog.hide();
                    }

                    @Override
                    public void positiveButton() {
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;

                    }

                    @Override
                    public void negativeButton() {
                        configBean.commonDialog.hide();
                        configBean.commonDialog = null;
                    }
                });
                configBean.commonDialog.show();
                break;
            case ResourceTable.Id_dl_seven:
                CharSequence[] words = {"路线一: 南京->上海->杭州", "路线二: 南京->武汉->长沙", "路线三: 南京->济南->天津", "路线四: 南京->合肥->重庆", "路线五: 南京->徐州->北京", "路线六: 南京->郑州->成都"};
                boolean[] selectItems = {true, false, false, false, false, false};

                ArrayList<ChooseBean> list2 = new ArrayList<>();
                for (int i = 0; i < words.length; i++) {
                    ChooseBean chooseBean = new ChooseBean();
                    chooseBean.setTxt(words[i]);
                    chooseBean.setChoosen(selectItems[i]);
                    list2.add(chooseBean);
                }
                configBean = StyledDialog.buildMySingleChoose(getContext(), "路线设置", "是", "否", list2, new ItemDialogListener() {
                    @Override
                    public void onItemClick(CharSequence text, int position) {
                        LogUtils.error(TAG, "position:"+position);
                        mPositon = position + 1;
                    }

                    @Override
                    public void positiveButton() {
                        configBean.commonDialog.hide();
                        if (mPositon == 0) {
                            ToastUtil.showToast(getContext(),"选择了第" + (mPositon+1) + "个");

                        } else {
                            ToastUtil.showToast(getContext(),"选择了第" + mPositon + "个");
                        }
                    }

                    @Override
                    public void negativeButton() {
                        configBean.commonDialog.hide();
                    }
                });
                configBean.commonDialog.show();
                break;
            default:

                break;
        }
    }
}
